package com.thinkgem.jeesite.modules.guobo.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.ExhOutInReport;
import com.thinkgem.jeesite.modules.guobo.entity.Exhibition;
import com.thinkgem.jeesite.modules.guobo.service.ExhHallService;
import com.thinkgem.jeesite.modules.guobo.service.ExhibitionService;
import com.thinkgem.jeesite.modules.guobo.service.TrailLogService;

/**
 * 场馆出入统计报表
 * @author wudavid
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/exhoutinreport")
public class ExhOutInReportController extends BaseController {
	
	@Autowired
	private TrailLogService trailLogService;
	
	@Autowired
	private ExhHallService exhHallService;
	
	@Autowired
	private ExhibitionService exhibitionService;
	
	/**
	 * 非固定资产
	 * @param exhOutInReport
	 * @param request
	 * @param choiceHall
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("guobo:exhoutinreport:view")
	@RequestMapping(value = {"list", ""})
	public String list(ExhOutInReport exhOutInReport, HttpServletRequest request,String[] choiceHall, HttpServletResponse response, Model model) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("assetCode", exhOutInReport.getAssetCode());
		param.put("assetName", exhOutInReport.getAssetName());
		param.put("assetSpec", exhOutInReport.getAssetSpec());
		param.put("assetModle", exhOutInReport.getAssetModle());
		/*param.put("startTime", exhOutInReport.getStartTime());
		param.put("endTime", exhOutInReport.getEndTime());*/
		String exhIds = "";		
		String exhibitionId = request.getParameter("exhibitionId");
		List<ExhHall> exhHalls = new ArrayList<ExhHall>();
		List<Exhibition> exhibitions = exhibitionService.findList(new Exhibition());
		model.addAttribute("exhibitions", exhibitions);
		model.addAttribute("exhibitionId", exhibitionId);
		if(exhibitionId!=null && !"".equals(exhibitionId) && !"null".equals(exhibitionId)){
			Exhibition exhibition = exhibitionService.get(exhibitionId);
			param.put("startTime", exhibition.getStarttime());
			param.put("endTime", exhibition.getEndtime());
			exhIds = exhibition.getExhIds();
			if(exhIds!=null && !"".equals(exhIds) && !"null".equals(exhIds)){
				param.put("exhIds", exhIds);
				String[] ids = exhIds.split(",");
				for (String id : ids) {
					exhHalls.add(exhHallService.get(id));
				}
			}
			
		}else{
			List<ExhHall> temps = exhHallService.findAllExhHall();
			 exhHalls.addAll(temps);
			 if(exhHalls!=null && exhHalls.size()>0){
				for (ExhHall exhHall : exhHalls) {
					exhIds = exhIds+","+exhHall.getId();
				}
				param.put("exhIds", exhIds.subSequence(1, exhIds.length()));
			}
		}
		/*List<Exhibition> exhibitions = exhibitionService.findList(new Exhibition());
		model.addAttribute("exhibitions", exhibitions);
		if(choiceHall==null || choiceHall.length==0){
			 List<ExhHall> temps = exhHallService.findAllExhHall();
			 exhHalls.addAll(temps);
		}else{		
			for (String id : choiceHall) {
				exhHalls.add(exhHallService.get(id));
			}
		}
		if(exhHalls!=null && exhHalls.size()>0){
			for (ExhHall exhHall : exhHalls) {
				exhIds = exhIds+","+exhHall.getId();
			}
			param.put("exhIds", exhIds.subSequence(1, exhIds.length()));
		}*/
		Collections.sort(exhHalls, new Comparator<ExhHall>() {
			@Override
			public int compare(ExhHall o1, ExhHall o2) {
				// 按照code升序排序
				if (Integer.parseInt(o1.getCode()) > Integer.parseInt(o2.getCode())) {
					return 1;
				}
				if (Integer.parseInt(o1.getCode()) == Integer.parseInt(o2.getCode())) {
					return 0;
				}
				return -1;
			}
		});
		Page<ExhOutInReport> page = trailLogService.getExhOutInReportByParam(new Page<ExhOutInReport>(request, response), exhOutInReport,param); 
		model.addAttribute("page", page);
		model.addAttribute("exhOutInReport", exhOutInReport);
		model.addAttribute("exhHalls", exhHalls);
		return "modules/guobo/exhOutInReport";
	}
	
	/**
	 * 固定资产
	 * @param exhOutInReport
	 * @param request
	 * @param choiceHall
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("guobo:exhoutinreport:view")
	@RequestMapping(value = {"fixedList", ""})
	public String fixedList(ExhOutInReport exhOutInReport, HttpServletRequest request,String[] choiceHall, HttpServletResponse response, Model model) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("assetCode", exhOutInReport.getAssetCode());
		param.put("assetName", exhOutInReport.getAssetName());
		param.put("assetSpec", exhOutInReport.getAssetSpec());
		param.put("assetModle", exhOutInReport.getAssetModle());
		/*param.put("startTime", exhOutInReport.getStartTime());
		param.put("endTime", exhOutInReport.getEndTime());*/
		String exhIds = "";		
		List<ExhHall> exhHalls = new ArrayList<ExhHall>();	
		String exhibitionId = request.getParameter("exhibitionId");
		List<Exhibition> exhibitions = exhibitionService.findList(new Exhibition());
		model.addAttribute("exhibitions", exhibitions);
		model.addAttribute("exhibitionId", exhibitionId);
		if(exhibitionId!=null && !"".equals(exhibitionId) && !"null".equals(exhibitionId)){
			Exhibition exhibition = exhibitionService.get(exhibitionId);
			param.put("startTime", exhibition.getStarttime());
			param.put("endTime", exhibition.getEndtime());
			exhIds = exhibition.getExhIds();
			if(exhIds!=null && !"".equals(exhIds) && !"null".equals(exhIds)){
				param.put("exhIds", exhIds);
				String[] ids = exhIds.split(",");
				for (String id : ids) {
					exhHalls.add(exhHallService.get(id));
				}
			}
			
		}else{
			List<ExhHall> temps = exhHallService.findAllExhHall();
			 exhHalls.addAll(temps);
			 if(exhHalls!=null && exhHalls.size()>0){
				for (ExhHall exhHall : exhHalls) {
					exhIds = exhIds+","+exhHall.getId();
				}
				param.put("exhIds", exhIds.subSequence(1, exhIds.length()));
			}
		}
		/*if(choiceHall==null || choiceHall.length==0){
			 List<ExhHall> temps = exhHallService.findAllExhHall();
			 exhHalls.addAll(temps);
		}else{		
			for (String id : choiceHall) {
				exhHalls.add(exhHallService.get(id));
			}
		}
		if(exhHalls!=null && exhHalls.size()>0){
			for (ExhHall exhHall : exhHalls) {
				exhIds = exhIds+","+exhHall.getId();
			}
			param.put("exhIds", exhIds.subSequence(1, exhIds.length()));
		}*/
		Collections.sort(exhHalls, new Comparator<ExhHall>() {
			@Override
			public int compare(ExhHall o1, ExhHall o2) {
				// 按照code升序排序
				if (Integer.parseInt(o1.getCode()) > Integer.parseInt(o2.getCode())) {
					return 1;
				}
				if (Integer.parseInt(o1.getCode()) == Integer.parseInt(o2.getCode())) {
					return 0;
				}
				return -1;
			}
		});
		//Page<ExhOutInReport> page = trailLogService.getExhOutInReport(new Page<ExhOutInReport>(request, response), exhOutInReport,param); 
		Page<ExhOutInReport> page = trailLogService.getFixedExhOutInReportByParam(new Page<ExhOutInReport>(request, response), exhOutInReport,param); 
		model.addAttribute("page", page);
		model.addAttribute("exhOutInReport", exhOutInReport);
		model.addAttribute("exhHalls", exhHalls);
		return "modules/guobo/exhFixedOutInReport";
	}
}
